import audioManager from "../game/audioManager";
import { GameCtrl } from "../game/gameCtrl";
import GameModel from "../game/gameModel";
import GameView from "../game/gameView";
// import Nakama from "../game/nakama";
import { ListenEventType } from "./defineConfig";
import OnEventMode from "./OnEventMode";

const { ccclass, property } = cc._decorator;

@ccclass
//事件控制类
export default class ONEventCtrl extends OnEventMode {

    private audioManager: audioManager = null
    private gameCtrl: GameCtrl = null
    private gameModel: GameModel = null
    private gameView: GameView = null
    // private nakama: Nakama = null

    //注册全部面板
    regesterModel(gameModel: GameModel, gameView: GameView, gameCtrl: GameCtrl, audioManager: audioManager) {
        this.audioManager = audioManager
        this.gameCtrl = gameCtrl
        this.gameView = gameView
        this.gameModel = gameModel
        // this.nakama = nakama
    }

    // 事件监听
    onEventListen() {
        this.bindEventByAudio()
        this.bindEventByView()
        this.bindEventByModel()
        this.bindEventCtrl()
        this.bindEventNetWork()
    }
    // 移除事件监听
    offEventListen() {
        this.unBindEventByAudio()
        this.unbindEventByView()
        this.unbindEventByModel()
        this.unBindEventCtrl()
        this.unBindEventNetWork()
    }

    private bindEventCtrl() {
        this.on(ListenEventType.RESTART_GAME, this.gameCtrl.restartGame, this.gameCtrl)   //重新开始游戏
    }

    private unBindEventCtrl() {
        this.off(ListenEventType.RESTART_GAME, this.gameCtrl.restartGame, this.gameCtrl)

    }

    private bindEventNetWork() {
        this.on(ListenEventType.NET_DATA, this.gameCtrl.recvNetData, this.gameCtrl)   //重新开始游戏
        this.on(ListenEventType.NET_DATA, this.gameModel.netDealCards, this.gameModel)     //数据层发牌
    }

    private unBindEventNetWork() {
        this.off(ListenEventType.NET_DATA, this.gameCtrl.recvNetData, this.gameCtrl)
        this.off(ListenEventType.NET_DATA, this.gameModel.netDealCards, this.gameModel)     //数据层发牌
    }

    private bindEventByAudio() {
        this.on(ListenEventType.PAUSE_MUSIC, this.audioManager.pauseBgMusic, this.audioManager)   //暂停音乐
        this.on(ListenEventType.PLAY_MUSIC, this.audioManager.resumeBgMusic, this.audioManager)   //播放音乐
        this.on(ListenEventType.PLAY_EFFECT, this.audioManager.playEffect, this.audioManager)     //播放音效
        this.on(ListenEventType.PAUSE_EFFECT, this.audioManager.pauseEffect, this.audioManager)   //暂停音效
        this.on(ListenEventType.INIT_AUDIO, this.audioManager.initAudio, this.audioManager)       //初始化audio

    }

    private unBindEventByAudio() {
        this.off(ListenEventType.PAUSE_MUSIC, this.audioManager.pauseBgMusic, this.audioManager)
        this.off(ListenEventType.PLAY_MUSIC, this.audioManager.resumeBgMusic, this.audioManager)
        this.off(ListenEventType.PLAY_EFFECT, this.audioManager.playEffect, this.audioManager)
        this.off(ListenEventType.PAUSE_EFFECT, this.audioManager.pauseEffect, this.audioManager)
        this.off(ListenEventType.INIT_AUDIO, this.audioManager.initAudio, this.audioManager)

    }

    private bindEventByView() {
        this.on(ListenEventType.INIT_CARD, this.gameView.initCards, this.gameView)      //初始化卡牌
        this.on(ListenEventType.GET_GAMEPANEL_BY_VIEW, this.gameView.getGamePanel, this.gameView)       //获取gamePanel
        this.on(ListenEventType.GET_POPUPPANEL_BY_VIEW, this.gameView.getPopupPanel, this.gameView)     //获取PopupPanel
        this.on(ListenEventType.UI_OPEN_SENDAREA_CARD, this.gameView.openSendAreaToRecycleArea, this.gameView)      ////通过View获取节点
        this.on(ListenEventType.UI_DEAL_CARDS, this.gameView.dealCards, this.gameView)      ////UI层翻开发牌区牌
        this.on(ListenEventType.RECYCLE_CARD, this.gameView.recycleCard, this.gameView)     //UI层打牌
        this.on(ListenEventType.SELECT_CARD, this.gameView.selectCard, this.gameView)      //UI层选择卡牌
        this.on(ListenEventType.POST_UI_CARD_COLOR, this.gameView.changeUIColor, this.gameView)     //通知UI层卡牌颜色
        this.on(ListenEventType.POST_UI_ROBOT_DEAL_CARD, this.gameView.robotDealCard, this.gameView)        //通知UI层机器人发牌
        this.on(ListenEventType.POST_UI_DEALAREA_TO_PLAYAREA, this.gameView.dealAreaToPlayArea, this.gameView)      //通知UI层将牌移至玩牌区
        this.on(ListenEventType.POST_UI_NEXT_BAR, this.gameView.changeUIBar, this.gameView)     //通知UI层轮到玩家的bar处理
        this.on(ListenEventType.POST_UI_SHOW_SELECTCOLOR, this.gameView.showSelectColorPanel, this.gameView)         //通知UI层显示选择颜色面板
        this.on(ListenEventType.POST_UI_UNO, this.gameView.postUNO, this.gameView)      //通知UI层UNO
        this.on(ListenEventType.POST_UI_POINTS, this.gameView.postPoints, this.gameView)      //通知UI层积分
        this.on(ListenEventType.POST_UI_SUITABLE_CARDS, this.gameView.postSuitableCards, this.gameView)      //通知UI层合适的牌
        this.on(ListenEventType.SHOW_GAMEOVER_PANEL, this.gameView.showGameoverPanel, this.gameView)        //显示游戏结束面板
        this.on(ListenEventType.RETURN_CARD_SPRITEFRAME_BY_TYPE, this.gameView.getCardByType, this.gameView)        //返回对应的卡牌图片
        this.on(ListenEventType.POST_UI_ISGAMEOVER, this.gameView.isGameOver, this.gameView)        //通知UI层是否游戏结束


    }

    private unbindEventByView() {
        this.off(ListenEventType.INIT_CARD, this.gameView.initCards, this.gameView)
        this.off(ListenEventType.GET_GAMEPANEL_BY_VIEW, this.gameView.getGamePanel, this.gameView)
        this.off(ListenEventType.UI_OPEN_SENDAREA_CARD, this.gameView.openSendAreaToRecycleArea, this.gameView)
        this.off(ListenEventType.GET_POPUPPANEL_BY_VIEW, this.gameView.getPopupPanel, this.gameView)
        this.off(ListenEventType.UI_DEAL_CARDS, this.gameView.dealCards, this.gameView)
        this.off(ListenEventType.RECYCLE_CARD, this.gameView.recycleCard, this.gameView)
        this.off(ListenEventType.SELECT_CARD, this.gameView.selectCard, this.gameView)
        this.off(ListenEventType.POST_UI_CARD_COLOR, this.gameView.changeUIColor, this.gameView)
        this.off(ListenEventType.POST_UI_ROBOT_DEAL_CARD, this.gameView.robotDealCard, this.gameView)
        this.off(ListenEventType.POST_UI_DEALAREA_TO_PLAYAREA, this.gameView.dealAreaToPlayArea, this.gameView)
        this.off(ListenEventType.POST_UI_NEXT_BAR, this.gameView.changeUIBar, this.gameView)
        this.off(ListenEventType.POST_UI_SHOW_SELECTCOLOR, this.gameView.showSelectColorPanel, this.gameView)
        this.off(ListenEventType.POST_UI_UNO, this.gameView.postUNO, this.gameView)
        this.off(ListenEventType.POST_UI_POINTS, this.gameView.postPoints, this.gameView)
        this.on(ListenEventType.POST_UI_SUITABLE_CARDS, this.gameView.postSuitableCards, this.gameView)      //通知UI层积分
        this.off(ListenEventType.SHOW_GAMEOVER_PANEL, this.gameView.showGameoverPanel, this.gameView)
        this.off(ListenEventType.RETURN_CARD_SPRITEFRAME_BY_TYPE, this.gameView.getCardByType, this.gameView)
        this.off(ListenEventType.POST_UI_ISGAMEOVER, this.gameView.isGameOver, this.gameView)



    }

    private bindEventByModel() {
        this.on(ListenEventType.MODEL_DEAL_CAEDS, this.gameModel.dealCards, this.gameModel)     //数据层发牌
        this.on(ListenEventType.POST_CARD_COLOR, this.gameModel.changeCardInf, this.gameModel)      //通知数据层卡牌颜色
        this.on(ListenEventType.POST_MODEL_PLAYERNUM, this.gameModel.initPlayerNum, this.gameModel)     //通知数据层玩家数目
        this.on(ListenEventType.CLICK_CARD, this.gameModel.clickCard, this.gameModel)       //点击卡牌
        this.on(ListenEventType.NEXT_PLAYER, this.gameModel.nestPlayerDealCard, this.gameModel)     //轮到下一位玩家出牌
        this.on(ListenEventType.ANIMATION_OVER, this.gameModel.aniamtionOver, this.gameModel) // 动画播放完毕
        this.on(ListenEventType.POST_MODEL_SELECT_COLOR, this.gameModel.alterColor, this.gameModel)     // 通知数据层修改颜色
        this.on(ListenEventType.POST_MODEL_ISUNO, this.gameModel.isUNO, this.gameModel)     //通知数据层是否UNO
        this.on(ListenEventType.GET_CARD_SPRITEFRAME_BY_TYPE, this.gameModel.getCardByType, this.gameModel)     //获取对应的卡牌图片
        this.on(ListenEventType.POST_MODEL_ISGAMEOVER, this.gameModel.postIsGameOver, this.gameModel)       //询问数据层是否游戏结束
    }

    private unbindEventByModel() {
        this.off(ListenEventType.MODEL_DEAL_CAEDS, this.gameModel.dealCards, this.gameModel)
        this.off(ListenEventType.POST_CARD_COLOR, this.gameModel.changeCardInf, this.gameModel)
        this.off(ListenEventType.POST_MODEL_PLAYERNUM, this.gameModel.initPlayerNum, this.gameModel)
        this.off(ListenEventType.CLICK_CARD, this.gameModel.clickCard, this.gameModel)
        this.off(ListenEventType.NEXT_PLAYER, this.gameModel.nestPlayerDealCard, this.gameModel)
        this.off(ListenEventType.ANIMATION_OVER, this.gameModel.aniamtionOver, this.gameModel)
        this.off(ListenEventType.POST_MODEL_SELECT_COLOR, this.gameModel.alterColor, this.gameModel)
        this.off(ListenEventType.POST_MODEL_ISUNO, this.gameModel.isUNO, this.gameModel)
        this.off(ListenEventType.GET_CARD_SPRITEFRAME_BY_TYPE, this.gameModel.getCardByType, this.gameModel)
        this.off(ListenEventType.POST_MODEL_ISGAMEOVER, this.gameModel.postIsGameOver, this.gameModel)


    }

}
